package test;

/*
 * Author: JacksonCoder
 * FileName: Arrays
 * Version: 1.0.0
 * Date: 2025/5/10
 */

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArraysDemo {

    public static void main(String[] args) {

        // 数组转化成 字符串
        int[] arr = {1, 2, 3, 4, 5, 6};
        System.out.println(Arrays.toString(arr));


        // sort()：对数组进行排序
        int[] arr1 = {4, 7, 1, 3, 2, 0, 9, 6, 8, 5};
        Arrays.sort(arr1);
        System.out.println("after sort arr1: " + Arrays.toString(arr1));

        int[] arr2 = {5, 2, 3, 1, 9, 6, 7};
        Arrays.sort(arr2, 1, 4);
        System.out.println(Arrays.toString(arr2));

        // binarySearch() : 使用二分查找算法在数组中搜索指定元素
        int[] arr3 = {1, 2, 5, 5, 6, 8, 9, 7};
        int index = Arrays.binarySearch(arr3, 5);
        System.out.println("index : " + index);

        int[] arr4 = {1, 2, 5, 5, 6, 9};
        int index1 = Arrays.binarySearch(arr4, 2, 5, 5);
        System.out.println("index1 : " + index1);

        // copyOf() 创建一个具有新产犊的数组副本
        int[] arr5 = {1, 2, 3, 4, 5};
        int[] newArr = Arrays.copyOf(arr5, arr5.length);
        System.out.println("newArr is :" + Arrays.toString(newArr));

        int[] arr6 = {1, 2, 3, 4, 5};
        int[] newArr1 = Arrays.copyOfRange(arr6, 1, 4);
        System.out.println(Arrays.toString(newArr1)); // 输出：[2, 3, 4]

        // fill()：用指定的值填充数组
        int[] arrays = new int[5];
        Arrays.fill(arrays, 10);
        System.out.println("arrays: " + Arrays.toString(arrays));

        int[] arrays1 = new int[5];
        Arrays.fill(arrays1, 10);
        System.out.println("arrays1: " + Arrays.toString(arrays1));

        // equals()：检查两个数组是否相等
        int[] a = {1, 2, 3, 4, 5};
        int[] b = {1, 2, 3, 4, 5};
        boolean isEqual = Arrays.equals(a, b);
        System.out.println("is equal: " + isEqual);

        // deepEquals() 比较两个多维数组是否深度相等
        int[][] dArr1 = {{1, 2}, {3, 4}};
        int[][] dArr2 = {{1, 2}, {3, 4}};
        boolean isDeepEqual = Arrays.deepEquals(dArr1, dArr2);
        System.out.println("is deep equal :" + isDeepEqual);

        // mismatch()：返回两个数组中第一个不匹配元素的索引
        int[] arraylist1 = {1,2,0,4,5};
        int[] arraylist2 = {1,2,3,4,5};
        // int mismatchIndex = Arrays.mismatch(arr1,arr2);   java9 引入

        // parallelSort(): 使用并行排序堆数组进行排序
        int[] arrs1 = {5,2,9,1,7};
        Arrays.parallelSort(arrs1);
        System.out.println("arrs1: " + Arrays.toString(arrs1));

        // asList() : 将数组转化为固定大小的列表
        int[] arraysList = {1,2,3,4,5};
        List<Integer> aList = Arrays.stream(arraysList)
                .boxed()
                .collect(Collectors.toList());
        System.out.println("aList :" + aList);

    }

}
